import java.util.*;

public class StudentCourseManager {
    private HashMap<Student, ArrayList<Course>> courseMap;

    public StudentCourseManager() {
        this.courseMap = new HashMap<>();
    }

    public void add(Student std, Course course) {
        courseMap.computeIfAbsent(std, k -> new ArrayList<>()).add(course);
    }

    public void showAll() {
        System.out.println("所有学生的选课信息:");
        for (Map.Entry<Student, ArrayList<Course>> entry : courseMap.entrySet()) {
            Student student = entry.getKey();
            ArrayList<Course> courses = entry.getValue();
            System.out.println(student);
            for (Course course : courses) {
                System.out.println("  " + course);
            }
        }
    }

    public void showStudent() {
        System.out.println("所有学生信息:");
        for (Student student : courseMap.keySet()) {
            System.out.println(student);
        }
    }

    public void showCourse() {
        System.out.println("所有课程信息:");
        Set<Course> allCourses = new HashSet<>();
        for (ArrayList<Course> courses : courseMap.values()) {
            allCourses.addAll(courses);
        }
        for (Course course : allCourses) {
            System.out.println(course);
        }
    }

    public ArrayList<Course> searching(Student std) {
        return courseMap.getOrDefault(std, new ArrayList<>());
    }

    public ArrayList<Student> searching(Course course) {
        ArrayList<Student> result = new ArrayList<>();
        for (Map.Entry<Student, ArrayList<Course>> entry : courseMap.entrySet()) {
            if (entry.getValue().contains(course)) {
                result.add(entry.getKey());
            }
        }
        return result;
    }
}    